package com.t2c.tibetantochinese;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.t2c.tibetantochinese.base.BaseActivity;
import com.t2c.tibetantochinese.home.HomeFragment;
import com.t2c.tibetantochinese.me.MeFragment;
import com.t2c.tibetantochinese.translate.TranslateFragment;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.utils.IntentUtil;
import com.t2c.tibetantochinese.utils.RealmUtil;
import com.t2c.tibetantochinese.utils.SPUtil;
import com.t2c.tibetantochinese.views.MyFragmentTabHost;

import java.io.IOException;

public class MainActivity extends BaseActivity {
    private LayoutInflater mLayoutInflater;
    private MyFragmentTabHost mTabHost;
    private Class mFragmentArray[] = {HomeFragment.class, TranslateFragment.class, MeFragment.class};

    private String mTextArray[] ;
    private int mImageArray[] = {R.drawable.main_selector, R.drawable.chinese_selector, R.drawable.me_selector};

    private boolean flag = true;//标记当前选项卡是否是homepage   是homepage为true  否则为false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!(boolean)SPUtil.get("save",false)){
            saveRealm();
        }
        initView();
    }

    //copy realm到data下
    private void saveRealm(){
        try {
            RealmUtil.copyBundledRealmFile(MainActivity.this,getAssets().open("raw/default.realm"),"default.realm");
            //存储标志，防止下次继续存储报错
            SPUtil.put("save",true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_main;
    }

    private void initView() {
        if(CommonUtil.isChinese(MainActivity.this)){
            mTextArray = new String[]{getResources().getString(R.string.homepage),
                    getResources().getString(R.string.chinese),
                    getResources().getString(R.string.my)};
        }else{
            mTextArray = new String[]{getResources().getString(R.string.thomepage),
                    getResources().getString(R.string.tchinese),
                    getResources().getString(R.string.tmy)};
        }
        mLayoutInflater = LayoutInflater.from(this);

        mTabHost = (MyFragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        int count = mFragmentArray.length;
        Bundle b = new Bundle();
        for (int i = 0; i < count; i++) {
            // 给每个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, mFragmentArray[i], b);
        }
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                flag = false;
                switch (tabId){
                    case "首页":
                        flag = true;
                        break;
                    case "མདུན་ངོས།":
                        flag = true;
                        break;
                    case "汉语":
                        SPUtil.put(MainActivity.this,"LANGUAGE","TIBETAN");
                        IntentUtil.gotoMain(MainActivity.this);
                        break;
                    case "རྒྱ་སྐད།":
                        SPUtil.put(MainActivity.this,"LANGUAGE","CHINESE");
                        IntentUtil.gotoMain(MainActivity.this);
                        break;
                    default:
                        break;
                }
            }
        });
        mTabHost.setCurrentTab(0);
    }

    private View getTabItemView(int index) {
        View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.textview);
        if(CommonUtil.isChinese(MainActivity.this)) {
            textView.setText(mTextArray[index]);
        }else{
            textView.setTypeface(CommonUtil.getTibetanType(MainActivity.this));
            textView.setText(mTextArray[index]);
        }
        return view;
    }

    @Override
    public void onBackPressed() {
        HomeFragment homeFragment = CommonUtil.getHomeFragment(MainActivity.this);
        //栈里的fragment数量大于1且当前界面为homepage时，执行回退方法，否则直接退出activity。
        if(homeFragment != null) {
            if (homeFragment.getChildFragmentManager().getBackStackEntryCount() > 1 && flag) {
                homeFragment.fragmentBack();
            } else {
                super.onBackPressed();
            }
        }
    }

}
